<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class LeaseOrderA extends Model
{

    /**
     * 数据表逻辑记录
     * 按时长计费计算租赁费用 ：
     * 1、使用时长（return_time【结束租赁时间】 - lease_time【开始租赁时间】 / 60 ） = 使用了多少分钟
     * 2、使用了多少分钟 / strategy_duration（时长周期） = 一共使用了多少个周期
     * 3、使用周期 * strategy_unit_price（单价） = 需要支付的租赁费用
     * 按次数计费计算租赁费用：
     * 1、strategy_unit_price（单价） = 需要支付的租赁费用
     * 按套餐计费计算租赁费用：
     * 1、strategy_unit_price（单价） = 需要支付的租赁费用
     *
     * 计算超时费用【只有按套餐计费需要支付超时费用】
     * 1、return_time（结束租赁时间） - limit_end_time（限制使用结束时间） > 0 =表示需要支付超时归还费用
     * 2、return_time（结束租赁时间） - limit_end_time（限制使用结束时间） / 60 = 超时时长（分钟）
     * 3、超时时长（分钟） / strategy_overtime_company（租赁超时收费单位） = 超时了几个单位
     * 4、超时了几个单位 * strategy_overtime_money（租赁超时收费金额） = 需要支付的租赁费用
     *
     * 免单逻辑
     * 1、使用时长（return_time【结束租赁时间】 - lease_time【开始租赁时间】 / 60 ） = 使用时长
     * 2、使用时长 < strategy_free_duration(免费试用时长) = 如果使用时长小于免费时长
     * 3、查询周期内免单的次数 strategy_free_cycle（免费周期） 天内免单了多少次 = 周期内已免单次数 查询order表
     * 4、strategy_free_frequency（免单次数） > 周期内已免单次数 =
     * 5、只有使用时长小于免单时长 及 周期内已经免单的次数小于最大可免单次数 才能免单
     */

    //use SoftDelete;

    // 表名
    protected $name = 'mycurrency_lease_order';

    // 追加属性
    protected $append = [
        'charging_text',
        //'use_status_text',
    ];

    const USE_STATUS_YIQUXIAO = -1;//使用状态:-1=已取消
    const USE_STATUS_MIANYA = 1;//使用状态:1=申请免押中
    const USE_STATUS_YAJIN = 2;//使用状态:2=支付押金中
    const USE_STATUS_ZHIFUXIANFU = 3;//使用状态:3=支付租赁费用中【先付后用】
    const USE_STATUS_JINXINGZHONG = 4;//使用状态:4=使用中
    const USE_STATUS_ZHIFUHOUFU = 5;//使用状态:5=支付租赁费用中【先用后付or补交费用】
    const USE_STATUS_YIWANCHENG = 6;//使用状态:6=已完成

    const CANCELL_TYPE_YAJINCHAOSHI = 1;//订单取消类型:1=押金支付超时
    const CANCELL_TYPE_ZUJINZHIFUCHAOSHI = 2;//订单取消类型:2=租赁费用支付超时
    const CANCELL_TYPE_ZULINSHIBAI = 3;//订单取消类型:3=设备租赁失败取消


    const STRATEGY_TYPES_SHICHANG = 1;//策略类型 = 按时长计费
    const STRATEGY_TYPES_CISHU = 2;//策略类型 = 按次数计费
    const STRATEGY_TYPES_TAOCAN = 3;//策略类型 = 按套餐计费

    const IS_FREE_BUMIANDAN = 1;//是否免单:1=不免单
    const IS_FREE_MIANDAN = 2;//是否免单:2=免单

    const PAY_STATUS_WEIZHIFU = 1;//支付状态:1=未支付
    const PAY_STATUS_YIZHIFU = 2;//支付状态:2=已支付


    //生成代理商唯一编号
    public static function getSn(){
        $number = date('YmdHis') . mt_rand(1000, 9999);
        $exists = self::where('sn', $number)->value('id');
        if (!$exists) {
            return $number;
        }
        return self::getSn();
    }


    /**
     * 1、用户下单
     * @param array $device 设备
     * @param int $user_id 下单用户id
     * @param int $strategy_package_id 套餐id
    */
    public static function orderAdd($device,$user_id,$strategy_package_id = null){
        $strategy_id = $device['lease_strategy_id'];//租赁策略id
        $strategy = LeaseStrategy::where(['id' => $strategy_id,'deletetime' => null])->find();
        if(!$strategy){
            throw new Exception('租赁策略不存在');
            return false;
        }
        if($strategy['types'] == LeaseStrategy::TYPES_TAOCAN){//策略类型 按套餐计费
            if($strategy_package_id == null){
                throw new Exception('套餐id不能为空');
                return false;
            }
            $package = LeaseStrategyPackage::where(['id' => $strategy_package_id,'deletetime' => null,'strategy_id' => $strategy['id']])->find();
            if(!$package){
                throw new Exception('套餐不存在');
                return false;
            }
            if($package['status'] != LeaseStrategyPackage::STATUS_ZHENGCHENG){
                throw new Exception('套餐不可用');
                return false;
            }
        }

        //生成主订单
        $increase = [
            'sn' => self::getSn(),//订单编号
            'user_id' => $user_id,//用户id
            'strategy_id' => $strategy->id,//计费策略id
            'strategy_types' => $strategy->types,//策略类型:1=按时长计费,2=按次数计费,3=按套餐计费
            'strategy_free_duration' => $strategy->free_duration,//免费试用时长（分）
            'strategy_free_frequency' => $strategy->free_frequency,//免费次数
            'strategy_free_cycle' => $strategy->free_cycle,//免费周期(天)
            'strategy_overtime_money' => $strategy->overtime_money,//租赁超时收费金额
            'strategy_overtime_company' => $strategy->overtime_company,//租赁超时收费单位
            'createtime' => time(),
        ];

        if ($strategy->types == LeaseStrategy::TYPES_SHICHANG){//策略类型:1=按时长计费
            $increase['strategy_duration'] = $strategy->duration;//时长周期
            $increase['strategy_unit_price'] = $strategy->unit_price;//单价
        }elseif ($strategy->types == LeaseStrategy::TYPES_CISHU){//策略类型:2=按次数计费
            $increase['strategy_unit_price'] = $strategy->unit_price;//单价
            $increase['rent_fee'] = $strategy->unit_price;//需支付租赁费用
            $increase['paymen_required'] = $strategy->unit_price;//实际总需要支付金额
            $increase['pending_payment'] = $strategy->unit_price;//待支付金额
        }elseif ($strategy->types == LeaseStrategy::TYPES_TAOCAN){//策略类型:3=按套餐计费
            $increase['strategy_package_id'] = $package->id;//计费套餐id
            $increase['strategy_duration'] = $package->duration;//时长周期
            $increase['strategy_unit_price'] = $package->money;//单价
            $increase['rent_fee'] = $package->money;//需支付租赁费用
            $increase['paymen_required'] = $package->money;//实际总需要支付金额
            $increase['pending_payment'] = $package->money;//待支付金额
        }
        $config = LeaseConfig::get(1);
        if($config['is_credit'] == LeaseConfig::IS_CREDIT_SHIYONG){//使用免押
            $increase['use_status'] = self::USE_STATUS_MIANYA;
        }elseif ($config['is_deposit'] == LeaseConfig::IS_DEPOSIT_JIAONA){//需要缴纳押金
            $increase['use_status'] = self::USE_STATUS_YAJIN;
        }elseif($config['pay_opportunity'] == LeaseConfig::PAY_OPPORTUNITY_XIANFU){//先付后用
            $increase['use_status'] = self::USE_STATUS_ZHIFUXIANFU;
        }else{//先用后付款
            $increase['use_status'] = self::USE_STATUS_JINXINGZHONG;
        }
        $order = self::create($increase);
        if(!$order){
            throw new Exception('订单生成失败');
            return false;
        }

        //添加设备使用记录
        $order_device = LeaseOrderDevice::deviceAdd($order->id,$device->id,$order->user_id);

        return $order->id;
    }

    /**
     * 订单取消
     * @param int $order_id 订单id
     * @param int $cancell_type 订单取消原因
    */
    public static function orderCancell($order_id,$cancell_type){
        $order = self::get($order_id);
        if (!$order){
            throw new Exception('订单不存在');
            return false;
        }
        $order_device = LeaseOrderDevice::where(['lease_order_id' => $order->id])->order('id desc')->find();
        if ($cancell_type == self::CANCELL_TYPE_YAJINCHAOSHI && $order->use_status != self::USE_STATUS_YAJIN){//支付押金超时
            throw new Exception('订单非支付押金中');
            return false;
        }
        if($cancell_type == self::CANCELL_TYPE_ZUJINZHIFUCHAOSHI && $order->use_status != self::USE_STATUS_ZHIFUXIANFU){//支付租赁费用超时
            throw new Exception('订单非支付租赁费用中');
            return false;
        }

        if ($cancell_type == self::CANCELL_TYPE_ZULINSHIBAI){//设备租赁失败取消
            if($order_device['status'] != LeaseOrderDevice::STATUS_ZULINZHONG){
                throw new Exception('设备非租赁中状态不能取消');
                return false;
            }
        }

        //修改主订单状态
        $order->use_status = self::USE_STATUS_YIQUXIAO;
        $order->cancell_type = $cancell_type;
        $order->cancell_time = time();
        $order->save();

        //修改设备租赁信息状态
        if($order_device){
            $order_device->status = LeaseOrderDevice::STATUS_QUXIAO;
            $order_device->save();

            Device::where(['id' => $order_device->device_id])->update([
                'use_status' => Device::USE_STATUS_KONGXIAN,
            ]);
        }

        //处理退款
        if ($cancell_type == self::CANCELL_TYPE_ZULINSHIBAI){//如果取消原因是设备租赁失败，要判断是否缴纳过了 押金 及支付费用 如果缴纳过需要原路退回
            if ($order['credit_id'] != null){//使用免押租赁
                //取消免押金
            }
            if ($order['deposit_id'] != null){//缴纳过押金
                LeaseDeposit::depositRefund($order['deposit_id']);
            }
            if($order->paid_dlready > 0){//支付过费用
                $order_pay = LeaseOrderPay::where([
                    'lease_order_id' => $order->id,
                    'pay_status' => LeaseOrderPay::PAY_STATUS__YIZHIFU,
                    'deletetime' => null,
                    'pay_opportunity' => LeaseOrderPay::PAY_OPPORTUNITY_SHIYONGQIAN
                ])->select();

                foreach($order_pay as $k => $v){
                    LeaseOrderPay::payRefund($v['id']);
                }
            }
        }
    }

    /**
     * 开始租赁设备
     * 1、下单后如果需要缴纳押金，缴纳押金支付成功后调用
     * 2、下单后如果需要先支付租赁费用，支付租赁费用后调用
     * 3、下单后如果使用免押，免押下单成功后调用
     * 4、如果是现租后付 ，下单成功后直接调用
     * @param int $order_id 订单id
    */
    public static function orderStartRenting($order_id){
        $order = self::get($order_id);
        if (!$order){
            throw new Exception('订单不存在');
            return false;
        }
        $order_device = LeaseOrderDevice::where(['lease_order_id' => $order->id])->order('id desc')->find();
        if ($order_device['status'] != LeaseOrderDevice::STATUS_WEIZULIN){
            throw new Exception('非待租赁状态');
            return false;
        }

        //调用设备模型控制设备指令 - 下发打开设备 未实现,

        //修改设备使用记录状态
        $order_device->status = LeaseOrderDevice::STATUS_ZULINZHONG;
        $order_device->save();
    }


    /**
     * 开始租赁设备 - 设备打开结果通知
     * @param int $order_device_id 租赁订单 设备使用记录id
     * @param int $device_result 打开结果 1 打开成功 2 打开失败【设备反馈故障打开失败或者设备通讯问题未反馈打开结果都视为打开失败，租赁订单不管为什么失败，具体失败类型有设备模块去记录处理】
    */
    public static function orderStartRentingResult($order_device_id,$device_result){
        $order_device = LeaseOrderDevice::where(['id' => $order_device_id])->find();
        if(!$order_device){
            throw new Exception('租赁订单设备使用记录不存在');
            return false;
        }
        if ($order_device['status'] != LeaseOrderDevice::STATUS_ZULINZHONG){
            throw new Exception('非租赁中状态');
            return false;
        }
    trace(["21========================处理lease_order_id报错问题_orderStartRentingResult==============================" => $order_device],'error');
        $order = self::get($order_device['lease_order_id']);
        if($device_result == 1){//租赁成功
            //修改租赁订单 设备使用记录状态
            $order_device->use_start_time = time();
            $order_device->status = LeaseOrderDevice::STATUS_SHIYONGZHONG;
            $order_device->save();

            //修改租赁订单状态
            if($order['strategy_types'] == self::STRATEGY_TYPES_TAOCAN){
                $order->limit_start_time = time();//限制使用开始时间
                $strategy = LeaseStrategy::get($order['strategy_id']);
                $package = LeaseStrategyPackage::get($order['strategy_package_id']);
                if ($strategy->package_types == LeaseStrategy::PACKAGE_TYPES_FENZHONG){//套餐时长类型:1=分钟
                    $order->limit_end_time = time() + ($package['duration'] * 60);//限制使用结束时间
                }elseif ($strategy->package_types == LeaseStrategy::PACKAGE_TYPES_XIAOSHI){//套餐时长类型:2=小时
                    $order->limit_end_time = time() + ($package['duration'] * 3600);//限制使用结束时间
                }elseif ($strategy->package_types == LeaseStrategy::PACKAGE_TYPES_TIAN){//套餐时长类型:3=天
                    $order->limit_end_time = time() + ($package['duration'] * 86400);//限制使用结束时间
                }
            }
            $order->lease_time = time();
            $order->save();
        }

        if ($device_result == 2){//租赁失败

            //取消租赁订单
            self::orderCancell($order->id,self::CANCELL_TYPE_ZULINSHIBAI);

            //修改租赁订单 设备使用记录状态
            //$order_device->status = LeaseOrderDevice::STATUS_QUXIAO;
            //$order_device->save();
        }
    }

    /**
     * 订单结束
     * @param int $order_id 订单id
     * @param int $return_time 订单结束时间戳 【为什么不读取当前系统时间？ 设备可能存在消息异常已规划订单没有结束的情况，这个时候需要后台去手动结束订单】
    */
    public static function orderEnd($order_id,$return_time){
        $order = self::where(['id' => $order_id,'deletetime' => null])->find();
        if(!$order){
            throw new Exception('订单不存在');
            return false;
        }
        if ($order['use_status'] != self::USE_STATUS_JINXINGZHONG){
            throw new Exception('订单非进行中状态');
            return false;
        }

        //租赁时长(分)
        $lease_duration =bcdiv(
            bcsub(
                $return_time,//结束租赁时间
                $order->lease_time,//开始租赁时间
            ),
            60,
            0
        );//开始租赁时间 - 结束租赁时间 / 60 = 租赁时长 分钟

        //租赁费用
        $rent_fee = $order->rent_fee;
        if($order->strategy_types == self::STRATEGY_TYPES_SHICHANG){//订单按时长计费
            $rent_fee = bcmul(
                ceil(//向上取整数
                    bcdiv(
                        $lease_duration,//租赁时长
                        $order->strategy_duration,//时长周期
                        2
                    )
                ),
                $order->strategy_unit_price,//单价
                2
            );
        }

        //超时费用
        $overtime_duration = 0;//租赁超时时长（分）
        $overtime_fee = 0;//归还超时需支付超时费用

        if($order->strategy_types == self::STRATEGY_TYPES_TAOCAN){//按套餐计费
            if($order->limit_end_time < $return_time){//限制归还时间小于实际规划时间，说明超时归还了
                $overtime_duration = ceil(//向上取整数
                    bcdiv(//得到分钟
                        bcsub(
                            $return_time,//实际归还时间
                            $order->limit_end_time//限制应归还时间
                        ),
                        60,
                        2
                    )
                );//超时归还时长 分钟
                $strategy_overtime_company =
                    ceil(//向上取整数
                        bcdiv(
                            $overtime_duration,//超时归还时长
                            $order->strategy_overtime_company,//租赁超时收费单位
                            2
                        )
                    );
                $overtime_fee = bcmul(
                    $strategy_overtime_company,
                    $order->strategy_overtime_money,//租赁超时收费金额
                    2
                );
            }
        }

        //是否免单
        $is_free = self::IS_FREE_BUMIANDAN;
        if($order->strategy_free_duration >= $lease_duration){//免单时长 大于租赁时长
            //查询用户指定时间段内免单次数 【根据归还时间查询】
            $start_time = $order->strategy_free_cycle - 86400;
            $end_time = $return_time;
            $order_free_count_where = [
                'user_id' => $order->user_id,
                'use_status' => self::USE_STATUS_YIWANCHENG,
                'is_free' => self::IS_FREE_MIANDAN,
                'return_time' => ['between',"{$start_time},{$end_time}"],
            ];
            $order_free_count = self::where($order_free_count_where)->count();
            if($order->strategy_free_frequency >= $order_free_count){//免单次数大于以免单的次数
                $is_free = self::IS_FREE_MIANDAN;
            }
        }

        $order->return_time = $return_time;//结束租赁时间
        $order->lease_duration = $lease_duration;//租赁时长(分)
        $order->overtime_duration = $overtime_duration;//租赁超时时长（分）
        $order->rent_fee = $rent_fee;//需支付租赁费用
        $order->overtime_fee = $overtime_fee;//归还超时需支付超时费用
        $order->paymen_required = bcadd($rent_fee,$overtime_fee,2);//实际总需要支付金额
        $order->pending_payment = bcsub($order->paymen_required,$order->paid_dlready,2);//待支付金额 ： 总支付金额 - 已支付金额 = 待支付金额
        //$order->use_status = self::USE_STATUS_ZULINZHONG;//使用状态
        $order->is_free = $is_free;//是否免单
        if($order->pending_payment <= 0){//待支付金额小于等于0 ： 如果订单是先付后用 并且没有超时 是不需要在支付的
            $order->use_status = self::USE_STATUS_YIWANCHENG;//使用状态 - 已完成
        }else{
            if ($order->is_free == self::IS_FREE_MIANDAN){//免单
                $order->use_status = self::USE_STATUS_YIWANCHENG;//使用状态 - 已完成
            }elseif ($order->is_free == self::IS_FREE_BUMIANDAN){//不免单
                $order->use_status = self::USE_STATUS_ZHIFUHOUFU;//使用状态 - 支付租赁费用中【先用后付or补交费用】
            }
        }

        $order->save();

        if($order->pending_payment <= 0){//待支付金额小于等于0 ： 如果订单是先付后用 并且没有超时 是不需要在支付的

        }else{
            if ($order->is_free == self::IS_FREE_MIANDAN){//免单
                if($order->credit_id != null){//如果有免押记录id 说明是免押订单
                    //免单需要取消免押订单
                }elseif ($order->deposit_id != null){//如果有押金记录id 说明使用了押金
                    //退回押金
                    LeaseDeposit::depositRefund($order->deposit_id);
                }
            }elseif ($order->is_free == self::IS_FREE_BUMIANDAN){//不免单
                if($order->credit_id != null){//如果有免押记录id 说明是免押订单
                    //调用免押扣款 - 免押接口需要返回成功扣款金额，如果扣款金额小于待支付金额 需要单独生成支付订单让用户支付，如果成功扣款金额等于待支付金额 订单状态调整为已完成
                }elseif ($order->deposit_id != null){//如果有押金记录id 说明使用了押金
                    //从押金中扣款
                    $deduction_fee = LeaseDeposit::deduction($order->deposit_id,$order->pending_payment);
                    $order->paid_dlready += $deduction_fee;//已支付金额
                    $order->pending_payment -= $deduction_fee;//待支付金额
                    if( $order->pending_payment <= 0){//如果待支付金额 小于等于0表示已全部支付完成
                        $order->use_status = self::USE_STATUS_YIWANCHENG;//订单状态 - 已完成
                    }
                    $order->save();
                    //添加押金支付记录
                    LeaseOrderPay::leaseDepositDeduction($order->id,$deduction_fee);
                    if($order->pending_payment > 0){//还有需要支付的金额
                        LeaseOrderPay::payAdd($order->id,LeaseOrderPay::PAY_OPPORTUNITY_SHIYONGHOU);
                    }
                }else{//正常支付
                    LeaseOrderPay::payAdd($order->id,LeaseOrderPay::PAY_OPPORTUNITY_SHIYONGHOU);
                }
            }
        }

    }

    /**
     * 订单计费方式
    */
    public function getChargingTextAttr($value, $data)
    {
        $str = '';
        if ($data['strategy_types'] == self::STRATEGY_TYPES_SHICHANG){//1=按时长计费
            $str = "{$data['strategy_unit_price']}元/{$data['strategy_duration']}分钟";
        }elseif ($data['strategy_types'] == self::STRATEGY_TYPES_CISHU){//1=按次数计费
            $str = "{$data['strategy_unit_price']}元/次";
        }elseif ($data['strategy_types'] == self::STRATEGY_TYPES_TAOCAN){//1=按套餐计费
            $package_types = LeaseStrategy::where(['id' => $data['strategy_id']])->value('package_types');
            if ($package_types == LeaseStrategy::PACKAGE_TYPES_FENZHONG){
                $package_types_str = '分钟';
            }elseif ($package_types == LeaseStrategy::PACKAGE_TYPES_XIAOSHI){
                $package_types_str = '小时';
            }elseif ($package_types == LeaseStrategy::PACKAGE_TYPES_TIAN){
                $package_types_str = '天';
            }
            $str = "{$data['strategy_unit_price']}元/{$package_types_str},超时归还{$data['strategy_overtime_money']}元/{$data['strategy_overtime_company']}分钟";
        }
        return $str;
    }

    /**
     * 订单 = 状态转文字
     */
    public static function getUseStatusText($data)
    {
        switch ($data->use_status) {
            case self::USE_STATUS_YIQUXIAO://-1=已取消
                $str = '已取消';
                if($data['cancell_type'] == self::CANCELL_TYPE_YAJINCHAOSHI){
                    $str .= '(押金支付超时)';
                }elseif ($data['cancell_type'] == self::CANCELL_TYPE_ZUJINZHIFUCHAOSHI){
                    $str .= '(租赁费用支付超时)';
                }elseif ($data['cancell_type'] == self::CANCELL_TYPE_ZULINSHIBAI){
                    $str .= '(设备租赁失败)';
                }
                break;
            case self::USE_STATUS_MIANYA://1=申请免押中
                $str = '待申请免押';
                break;
            case self::USE_STATUS_YAJIN://2=支付押金中
                $str = '待支付押金';
                break;
            case self::USE_STATUS_ZHIFUXIANFU://3=支付租赁费用中【先付后用】
                $str = '待支付租金';
                break;
            case self::USE_STATUS_JINXINGZHONG://4=使用中
                $str = '使用中';
                if ($data['rentingingrogressdevice']['status'] == LeaseOrderDevice::STATUS_WEIZULIN){
                    $str .= '(未租赁)';
                }elseif ($data['rentingingrogressdevice']['status'] == LeaseOrderDevice::STATUS_ZULINZHONG){
                    $str .= '(租赁中)';
                }elseif ($data['rentingingrogressdevice']['status'] == LeaseOrderDevice::STATUS_GUIHUANZHONG){
                    $str .= '(归还中)';
                }elseif ($data['rentingingrogressdevice']['status'] == LeaseOrderDevice::STATUS_YIGUIHUAN){
                    $str .= '(已归还)';
                }
                break;
            case self::USE_STATUS_ZHIFUHOUFU://5=支付租赁费用中【先用后付or补交费用】
                $str = '待支付';
                break;
            case self::USE_STATUS_YIWANCHENG://6=已完成
                $str = '已完成';
                break;
            default:
                break;
        }
        $data['use_status_text'] = $str;
        return $data;
    }

    /**
     * 操作权限
    */
    public static function operationJurisdiction($data){
        $is_credit = 0;//调起免押 0 = 不可操作 1 = 可以操作
        $is_deposit = 0;//押金支付 0 = 不可操作 1 = 可以操作
        $is_payfirst = 0;//操作付款【先用后付及先付后用】 0 = 不可操作 1 = 可以操作
        $is_return = 0;//归还 0 = 不可操作 1 = 可以操作
        if($data->use_status == self::USE_STATUS_MIANYA){
            $is_credit = 1;
        }
        if ($data->use_status == self::USE_STATUS_YAJIN){
            $is_deposit = 1;
        }
        if ($data->use_status == self::USE_STATUS_ZHIFUXIANFU || $data->use_status == self::USE_STATUS_ZHIFUHOUFU){
            $is_payfirst = 1;
        }
        if ($data->use_status == self::USE_STATUS_JINXINGZHONG){
            $is_return = 1;
        }
        return [
            'is_credit' => $is_credit,
            'is_deposit' => $is_deposit,
            'is_payfirst' => $is_payfirst,
            'is_return' => $is_return,
        ];
    }

    public function getLimitStartTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getLimitEndTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getLeaseTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function getReturnTimeAttr($value){
        return $value != null ? date('Y-m-d', $value) : '';
    }

    public function deposit(){
        return $this->hasOne(LeaseDeposit::class, 'id', 'deposit_id');
    }

    public function pendingPayment(){
        return $this->hasOne(LeaseOrderPay::class, 'lease_order_id', 'id');
    }

    public function rentingingrogressdevice(){
        return $this->hasOne(LeaseOrderDevice::class, 'lease_order_id', 'id');
    }


}
